import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken } from "@/utils/auth"; // get token from cookie
import getPageTitle from "@/utils/get-page-title";
import { userRoutes, constantRoutes } from "./router/index";

import Layout from "@/layout";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = [
  "/login",
  "/index",
  "/userLogin",
  // "/404",
  "/shopDetails",
  "/register",
  "/Reset",
]; // no redirect whitelist
const arrays = ["/person"]; //需要校验的
router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start();

  // set page title
  document.title = getPageTitle(to.meta.title);

  // determine whether the user has logged in

  const hasToken = getToken();
  // console.log(to.path, "pathyy");
  if (to.path == "/userLogin") {
    next();
    // NProgress.done();
  } else {
    if (!arrays.includes(to.path) && to.path != "/login") {
    } else if (to.path == "/login") {
      next();
    } else if (arrays.includes(to.path)) {
      console.log(to.path, "path1111");
      if (localStorage.getItem("myToken")) {
        console.log("333333");
        next();
      } else {
        next("/userLogin");
        NProgress.done();
      }
    }
  }

  if (hasToken != undefined && hasToken) {
    if (to.path === "/login") {
      // if is logged in, redirect to the home page
      // next({ path: "/" });
      // NProgress.done();
      next({ path: "/" });
      NProgress.done();
    }
    // else if(!arrays.includes(to.path)){
    //   console.log(to.path,'path')
    // }
    else {
      const hasGetUserInfo = store.getters.name;
      console.log(hasGetUserInfo, "info");
      
      if (hasGetUserInfo) {
        next();
        router.options.routes = constantRoutes.slice(0, constantRoutes.length);
      } else {
        try {
          // get user info
          console.log(store.state.user.ident,'pppp')
          await store.dispatch("user/getInfo");
          if (store.state.user.ident == "1") {
            console.log(store.state.user.ident,'iioopp')
            //取管理员1路由
            router.addRoutes(userRoutes);
            //前边加上模块名
            store.commit("router/SET_ROUTERS", userRoutes);
            console.log(router,'11111')
            // next();
            next({...to,replace: true})
          } else if (store.state.user.ident == "2") {
            //取管理员2路由
            // console.log(store.state.router.routes, "kkll");
            next();
          }
          //获取路由
          // const arr = [
          //   {
          //     path: '/form',
          //     component: Layout,
          //     children: [
          //       {
          //         path: 'index',
          //         name: 'Form',
          //         component: () => import('@/views/form/index'),
          //         meta: { title: 'Form', icon: 'form' }
          //       }
          //     ]
          //   },
          // ]
          // router.addRoutes(arr)
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch("user/resetToken");
          Message.error(error || "Has Error");
          next(`/login?redirect=${to.path}`);
          NProgress.done();
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next();
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      if (!arrays.includes(to.path)) {
        // next(`/login?redirect=${to.path}`);
        next("/userLogin");
        NProgress.done();
      } else {
        if (localStorage.getItem("myToken")) {
          next();
          return;
        }
        next("/userLogin");
      }
    }
  }
});

router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
